import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class InviteKocLogic extends GetxController {
  final state = InviteKocState();
  @override
  void onInit() {
    super.onInit();

    print('**************************');
    print('leaderUsrNumId: ${SpUtil.getInt('empeNumId')}');
    print('**************************');
    state.phone=SpUtil.getString('number');
  }

  @override
  void onReady() {
    super.onReady();

    /// 获取二维码
    // var params = {
    //   "qrcode_media_type":2,
    //   "qrcode_type":6,
    //   "related_entity_id":64,
    //   "valid_type":5,
    //   "valid_length":30,
    //   "datajson":"{'parentMobile':'15737716101'}",
    //   "miniprogram_code":{
    //     "appid": "wx8a60f407910784b9",
    //     "page": "pages/koc/createKOC"
    //   }
    // };

    var params1 = {
      'usr_num_ids': [SpUtil.getInt('empeNumId')],
    };

    var params = {
      "qrcode_media_type": 2,
      "qrcode_type": 0,
      "content": null,
      "miniprogram_code": {
        "appid": Config.MINI_APPID,
        "scene": "",
        "page": "pages/login"
      },
      "related_entity_id": 3961529,
      "valid_type": 1,
      "valid_length": null,
      "datajson": "{\"applyer\":${SpUtil.getInt('empeNumId')}}",
      "recreate_sign": 0
    };
    print('**************************');
    print(params);
    print('**************************');
    EasyLoading.show();
    Api.kocQrcode(params).listen((event) {
      state.qrCode = event['qrcode']['qrcode_url'];
      EasyLoading.dismiss();

      update();
    });
    // Api.userDetail(params1).listen((event) {
    //   if (event['distribution_info_list'].length == 0) return;
    //   if (event['code'] == 0) {
    //     state.phone = event['distribution_info_list'][0]['mobile_phone'];
    //     var params = {
    //       "qrcode_media_type": 2,
    //       "qrcode_type": 0,
    //       "content": null,
    //       "miniprogram_code": {
    //         "appid": Config.MINI_APPID,
    //         "scene": "",
    //         "page": "pages/login"
    //       },
    //       "related_entity_id": 3961529,
    //       "valid_type": 1,
    //       "valid_length": null,
    //       "datajson": "{\"applyer\":${SpUtil.getInt('empeNumId')}}",
    //       "recreate_sign": 0
    //     };
    //     print('**************************');
    //     print(params);
    //     print('**************************');
    //     Api.kocQrcode(params).listen((event) {
    //       state.qrCode = event['qrcode']['qrcode_url'];
    //       update();
    //     });

    //     // Api.kocImg({}).listen((event) {
    //     //   var data = event['distribution_poster_setting'];
    //     //   print('*****************');
    //     //   print(data);
    //     //   state.imgUrl = data['invite_card_image'];
    //     //   state.text = data['invite_card_recommend_word'];
    //     //   state.color = data['invite_card_text_color'];
    //     //   print(event);
    //     //   update();
    //     // });
    //   }
    //   update();
    // });
    update();
  }

  String get miniPath {
    return 'pages/login?applyer=${SpUtil.getInt('empeNumId')}';
  }
}
